// components/findlost/findlost.js
//需要联系方式的输入框
const app = getApp()
const db = wx.cloud.database()
var sev = require('../../../../../utils/sev.js');
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    imgList: [],
    btnhide: false,
    content: '',
    cloudlist: [],
    readurllist: [],
    title: '',
    calls:''//联系方式
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getCalls(e){
      this.setData({
        calls:e.detail.value
      })
    },
    getTitle(e) {
      this.setData({
        title: e.detail.value
      });
    },
    textareaAInput(e) {
      this.setData({
        content: e.detail.value
      }); //赋值
    },
    updata() {
      wx.showLoading({
        title: '上传中~',
        mask: true
      })
      //上传图片
      var that = this;
      that.setData({
        btnhide: true
      })
      var timecode = sev.vcode(new Date());
      var files = this.data.imgList;
      var len = files.length;
      var i = 0;
      for (i = 0; i < len; i++) {
        wx.cloud.uploadFile({
          cloudPath: 'LOST/' + timecode + i + '.png',
          filePath: files[i],
          success(res) {LOST
            // console.log(res)
            //加入cloudlist列表
            that.setData({
              cloudlist: that.data.cloudlist.concat([res.fileID]),
            })
          },
          fail(res) {
            console.log(res)
          }
        })
      }
      var timer = setInterval(function() {
        // console.log(that.data.cloudlist.length)
        if (that.data.cloudlist.length == len) {
          clearInterval(timer);
          that.toUpdate();
        }
      }, 1000)
    },
    toUpdate() {
      var that = this;
      wx.cloud.getTempFileURL({
        fileList: that.data.cloudlist,
        success(res) {
          var turls = [];
          for (var i = 0; i < res.fileList.length; i++) {
            turls.push(res.fileList[i].tempFileURL);
          }
          var timer = setInterval(function() {
            if (res.fileList.length == turls.length) {
              clearInterval(timer);
              that.setData({
                readurllist: turls
              }, () => {
                //掉用函数上传
                wx.cloud.callFunction({
                  name: 'kindb',
                  data: {
                    tablename: 'LOST',
                    opr: 'add',
                    data: {
                      title: that.data.title,
                      content: that.data.content,
                      dbdate: sev.formatTime(new Date()),
                      cloudlist: that.data.cloudlist,
                      realList: that.data.readurllist,
                      calls:that.data.calls,
                    }
                  },
                  success(_res) {
                    wx.hideLoading();
                    wx.navigateBack({
                      //返回上一页
                    })
                    console.log(_res)
                  },
                  fail(_res) {
                    wx.showModal({
                      title: '上传失败',
                      content: '',
                      showCancel: false,
                      success(tres) {
                        if (tres.confirm) {
                          wx.navigateBack({
                            //返回上一层
                          })
                        }
                      }
                    })
                  }
                })
              })
            }
          }, 1000)
        }
      })


    },
    ChooseImage() {
      wx.chooseImage({
        count: 3, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (res) => {
          if (this.data.imgList.length != 0) {
            this.setData({
              imgList: this.data.imgList.concat(res.tempFilePaths)
            })
          } else {
            this.setData({
              imgList: res.tempFilePaths
            })
          }
        }
      });
    },
    ViewImage(e) {
      wx.previewImage({
        urls: this.data.imgList,
        current: e.currentTarget.dataset.url
      });
    },
    DelImg(e) {

      this.data.imgList.splice(e.currentTarget.dataset.index, 1);
      this.setData({
        imgList: this.data.imgList
      })

    }
  }
})